﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;
using Whir.Common;
using System.Collections.Specialized;
using Whir.Common.Resource;
using System.Xml;

public partial class Whir_System_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string test = Whir.Common.Settings.GetString("XmlPath") + "SiteConfig.xml";
            string xmlPath = Server.MapPath("~"+ Whir.Common.Settings.GetString("XmlPath") + "SiteConfig.xml");

    
            //string xmlPath = Server.MapPath(Whir.Common.Settings.GetString("XmlPath") + "SiteConfig.xml");


            Type type = typeof(Whir.Config.Model.SiteConfig);
            Whir.Config.Model.SiteConfig model = (Whir.Config.Model.SiteConfig)Whir.Common.XmlHelper.DeserializeObject(xmlPath, type);         //反序列化

            //litImg.Text = String.Format("<img src='{0}' width='289' height='216' />", Whir.Common.Settings.GetPicServer() + model.bannerUrl);
            litImg.Text = String.Format("<img src='{0}' width='289' height='216' />", ResolveClientUrl("~" + Whir.Common.Settings.GetPicServer() + model.bannerUrl));
             

            this.txt1.Focus();
            BuildDrawDownList();
        }
    }

    /// <summary>
    /// 生成语言选择下拉列表
    /// </summary>
    private void BuildDrawDownList()
    {
        NameValueCollection languages = ResourceManager.GetSupportedLanguages();
        for (int i = 0; i < languages.Count; i++)
        {
            ListItem item = new ListItem(languages[i], languages.Keys[i]);
            ddlLanguage.Items.Add(item);
        }
    }

    protected void ibnLogin_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["CheckCode"] == null)
        {
            Whir.Common.WebHelper.JsHelper.Alert(Page, "  验证码过期，请点击刷新  ");
            return;
        }

        //判断验证码
        if (this.txt3.Text != Session["CheckCode"].ToString())
        {
            Whir.Common.WebHelper.JsHelper.Alert(Page, "验证码不正确!");
            this.txt2.Focus();
            this.txt3.Text = "";
            return;
        }
        //end
        Session["CheckCode"] = null;

        ////是否需要更改AppSetting
        //if (IsChange())
        //{
        //    Whir.Common.WebHelper.JsHelper.Alert(Page, "Config/Settings.config里的虚拟目录需要改成当前项目的目录！");
        //    return;
        //}

        //IP限制
        bool IsCanLogin = true;
        //        IsCanLogin = RoleHelper.IsCanLogin();

        //验证用户名和密码是否过滤
        bool IsCanSQLInjection = Whir.Common.WebHelper.NetRequest.FilterSql(txt1.Text);

        if (IsCanLogin && IsCanSQLInjection)
        {            
            DataSet ds = Whir.SQLServerDAL.Whir_AdminInfo.AdminLogin(this.txt1.Text, this.txt2.Text);

            if (ds.Tables[0].Rows.Count > 0)
            {
                Whir.Model.Whir_AdminInfo model = new Whir.Model.Whir_AdminInfo();

                model.adminID = int.Parse(ds.Tables[0].Rows[0]["adminID"].ToString());
                model.adminName = ds.Tables[0].Rows[0]["adminName"].ToString();
                model.adminPassword = ds.Tables[0].Rows[0]["adminPassword"].ToString();
                model.adminTrueName = ds.Tables[0].Rows[0]["adminTrueName"].ToString();
                model.roleIDs = ds.Tables[0].Rows[0]["roleIDs"].ToString();
                model.typeID = int.Parse(ds.Tables[0].Rows[0]["typeID"].ToString());
                model.createDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["createDate"].ToString());
                model.createUserID = int.Parse(ds.Tables[0].Rows[0]["createUserID"].ToString());
                model.roleValue = RoleHelper.GetRoleValue(model.roleIDs);

                string syscache = Whir.Common.Settings.GetString("AdminCacheKey");

                //保存Cookie
                Whir.Common.WebHelper.StringHelper.WriteCookie(syscache, "user-" + model.adminID.ToString(), 90);

                OLAdd();

                //语言版本
                Session[Settings.GetString("languageSessionKey")] = ddlLanguage.SelectedValue;

                Response.Redirect("Default.aspx");
            }
            else
            {
                Whir.Common.WebHelper.JsHelper.Alert(Page, "帐号或密码不正确!");
                this.txt1.Focus();
                this.txt3.Text = "";
                return;
            }            
        }
        else
        {
            Whir.Common.WebHelper.JsHelper.Alert(Page, "当前IP禁止访问网站后台!");
        }
    }

    //操作日志
    protected void OLAdd()
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "登录";
        model.operateType = 4;      //登陆
        model.operateResult = true;
        model.operateDesc = "登录";

        Whir.Log.OperateLog.Add(model);
    }

    protected bool IsChange()
    {
        bool flag = true;
        string m_ApplicationPath = Request.ApplicationPath;
        if (m_ApplicationPath == "")
            m_ApplicationPath = "/";
        if (!m_ApplicationPath.EndsWith("/"))
            m_ApplicationPath += "/";

        XmlDocument document = new XmlDocument();
        document.PreserveWhitespace = true;
        string filename = HttpContext.Current.Server.MapPath("~/Config/Settings.config");
        document.Load(filename);

        foreach (XmlNode node2 in document.SelectSingleNode("appSettings"))
        {
            if (node2.Name == "add")
            {
                XmlAttribute attribute = node2.Attributes["key"];
                if (attribute != null)
                {
                    if (attribute.Value == "PicServer" || attribute.Value == "ItemPicPath"
                        || attribute.Value == "AdminPath" || attribute.Value == "SystemPath"
                        || attribute.Value == "XmlPath" || attribute.Value == "FCKeditor:UserFilesPath")
                    {
                        XmlAttribute attribute2 = node2.Attributes["value"];
                        if (attribute2 != null)
                        {
                            if (attribute2.Value.Contains("Release"))
                            {
                                flag = false;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }
}
